package cn.com.cetccst.activation.ui;

import static com.google.android.gms.common.util.CollectionUtils.listOf;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import androidx.appcompat.app.AppCompatActivity;

import com.google.zxing.BarcodeFormat;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.journeyapps.barcodescanner.DefaultDecoderFactory;

import java.util.Collection;

import cn.com.cetccst.activation.R;

public class ScanQRActivity extends AppCompatActivity {

    public static final String INTENT_CAPTURE_CONTENT = "intent_capture_content";
    private DecoratedBarcodeView barcodeView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_qr);
        barcodeView = findViewById(R.id.dbv_mg);
        //监听扫描结果
        barcodeView.decodeContinuous(result -> {
            if(!result.getText().isEmpty()) {
                Intent intent = new Intent();
                intent.putExtra(INTENT_CAPTURE_CONTENT,result.getText());
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return barcodeView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        barcodeView.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        barcodeView.pause();
    }
}
